package com.gljx.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gljx.util.CommonExport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.gljx.util.GetFid;
import com.gljx.util.GetFnumber;
import com.gljx.util.TimeFormat;
import com.gljx.web.Entity.Department;
import com.gljx.web.Entity.OtherRefundItem;
import com.gljx.web.Entity.User;
import com.gljx.web.service.CustomLogService;
import com.gljx.web.service.DepartmentService;
import com.gljx.web.service.OtherRefundItemService;

@Controller
@RequestMapping("/")
public class OtherRefundItemController {

	//自动注入
	@Autowired
	private OtherRefundItemService otherRefundItemService;
	
	//自动注入
	@Autowired
	private DepartmentService departmentService;
	
	//自动注入
	@Autowired
	private CustomLogService customLogService;
	
	/**
	 * 根据公司查询所有的其他退款项目
	 * @param request
	 * @return
	 */
	@RequestMapping("/queryOtherRefundItem/look.action")
    public String queryOtherRefundItem(HttpServletRequest request, String flag) {
        //获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
		//查询数据
		List<OtherRefundItem> otherRefundItems = otherRefundItemService.queryOtherRefundItemByCompanyId(user.getFcompanyid());
		//把数据传到页面
		request.setAttribute("otherRefundItems", otherRefundItems);
        request.setAttribute("flag", flag);
        return "/backHome/paramentData/otherRefundItems/list";
	}
	
	/**
	 * 根据名称查询其他退款项目
	 * @param request
	 * @param fnameL2
	 * @return
	 */
	@RequestMapping("/queryOtherRefundItemByFnameL2")
	public String queryOtherRefundItemByFnameL2(HttpServletRequest request,String fnameL2){
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
        //去除空格
        fnameL2 = fnameL2.trim();
        //查询数据
		List<OtherRefundItem> otherRefundItems = otherRefundItemService.queryOtherRefundItemByCompanyIdAndName(user.getFcompanyid(), fnameL2);
		//把数据传到页面
		request.setAttribute("otherRefundItems", otherRefundItems);
		request.setAttribute("fnameL2", fnameL2);
		return "/backHome/paramentData/otherRefundItems/list";
	}
	
	/**
	 * 跳转到添加页面
	 * @param request
	 * @param type
	 * @return
	 */
	@RequestMapping("/insertOtherRefundItem")
	public String insertOtherRefundItem(HttpServletRequest request,String type){
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
        /*String number = customLogService.selectFunmber("CT_DS_OTHERREFYNDITEM", user.getFcompanyid());*/
	    //根据上一个编码获取编码
        String fnumber = GetFnumber.fnumbers("QTTKXM");
	    //获取组织
	  	Department department = departmentService.queryAdminByFid(user.getFcompanyid());
	  	//把数据传到页面
		request.setAttribute("department", department);
	  	request.setAttribute("fnumber", fnumber);
	  	request.setAttribute("type", type);
		return "/backHome/paramentData/otherRefundItems/edit";
	}
	
	/**
	 * 添加其他退款项目
	 * @param request
	 * @param otherRefundItem
	 * @param fid
	 * @return
	 */
	@RequestMapping("/editOtherRefundItem")
	public String editOtherRefundItem(HttpServletRequest request,OtherRefundItem otherRefundItem,String fid){
		//获取操作人信息
		User user = (User)request.getSession().getAttribute("user");
	    //公司id
		otherRefundItem.setCfficompanyid(user.getFcompanyid());
		//控制单元id
		otherRefundItem.setFcontrolunitid(user.getFcompanyid());
	    //获取当前时间
	  	String time = TimeFormat.timeFormat(new Date());
	  	if(fid == null || "".equals(fid)){
			//利用UUID生成fid
	  	    otherRefundItem.setFid(GetFid.UUIDIntoFid());
	  		//当前创建时间
	  		otherRefundItem.setFcreatetime(time);
		    //创建人id
	  	    otherRefundItem.setFcreatorid(user.getFid());
		    //完成添加
	  	    otherRefundItemService.add(otherRefundItem);
		    //添加日志
		    customLogService.addLog("财务基础资料", time+"对其他退款收费项目进行添加操作", null, user.getFid());
		    //重定向到添加方法
		    return "redirect:/DrivingSys/insertOtherRefundItem?type=3";
	  	}else{
	  		//修改时间
	  		otherRefundItem.setFlastupdatetime(time);
	  		//修改人
	  		otherRefundItem.setFlastupdateuserid(user.getFid());
	  		//修改
	  		otherRefundItemService.modify(otherRefundItem);
	  		//添加日志
			customLogService.addLog("财务基础资料", time+"对其他退款收费项目进行修改操作", null, user.getFid());
		    //重定向到查询页面
            return commonController.skipUrl("/queryOtherRefundItem/look.action");
        }
	}
	
	/**
	 * 根据fid查询
	 * @param request
	 * @param fid
	 * @return
	 */
	@RequestMapping("/selectOtherRefundItemByFid")
	public String selectOtherRefundItemByFid(HttpServletRequest request,String fid){
		//获取操作人信息
	    User user = (User)request.getSession().getAttribute("user");
	    //获取组织
	  	Department department = departmentService.queryAdminByFid(user.getFcompanyid());
	  	//根据fid查询
	  	OtherRefundItem otherRefundItem = otherRefundItemService.queryByPrimaryKey(fid);
	  	//把数据传到页面
	  	request.setAttribute("otherRefundItem", otherRefundItem);
	  	request.setAttribute("department", department);
		return "/backHome/paramentData/otherRefundItems/edit";
	}
	
	/**
	 * 根据fid删除
	 * @param request
	 * @param deleteId
	 * @param fid
	 * @return
	 */
	@RequestMapping("/deleteOtherRefundItemByFid")
	public String deleteOtherRefundItemByFid(HttpServletRequest request,String[] deleteId,String fid){
		//获取操作人信息
	    User user = (User)request.getSession().getAttribute("user");
	    //删除
	    otherRefundItemService.deleteByFid(deleteId,fid);
	    //添加日志
	    customLogService.addLog("财务基础资料", TimeFormat.timeFormat(new Date())+"对其他退款收费项目进行删除操作", null, user.getFid());
		//重定向到查询页面
	    return "redirect:/DrivingSys/queryOtherRefundItem/look.action";
	}

	/**
	 * 导出Excel
	 *
	 * @param response
	 * @param request
	 * @param fnameL2
	 */
	@RequestMapping("/importOtherRefundItem")
	public void importOtherRefundItem(HttpServletResponse response, HttpServletRequest request, String fnameL2) {
		//获取操作人信息
		User user = (User) request.getSession().getAttribute("user");
		//定义接收类型
		List<Object> data = new ArrayList<>();
		//查询数据
        List<OtherRefundItem> otherRefundItems = otherRefundItemService.queryOtherRefundItemByCompanyIdAndName(user.getFcompanyid(), fnameL2.trim());
        for (OtherRefundItem otherRefundItem : otherRefundItems) {
			data.add(otherRefundItem.getFnumber());
			data.add(otherRefundItem.getFnameL2());
			data.add(otherRefundItem.getFsimplename());
			data.add(otherRefundItem.getFdescriptionL2());
			data.add(otherRefundItem.getCompanyName());
			data.add(otherRefundItem.getFcreatetime());
			data.add(otherRefundItem.getFlastupdatetime());
			data.add(otherRefundItem.getCreateName());
			data.add(otherRefundItem.getUpdateName());
			data.add(otherRefundItem.getCfamount());
		}
		//头部数据
		String head[] = {"编码", "名称", "简称", "描述", "驾校", "创建时间", "最后修改时间", "创建者", "修改人", "数值"};
		//长度
		Integer length[] = {9000, 7000, 6000, 6000, 7000, 6000, 6000, 3000, 3000, 2000};
		try {
			//导出Excel
			CommonExport.importExcel(response, "其他退款项目" + TimeFormat.getNowTime2() + ".xls", "其他退款项目", data, head, length);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
